<?php

namespace App\Http\Controllers\Admin;

use App\Models\ProdOrder;
use App\Models\Transition;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

/**
 * @group 后台接口文档
 * @title 订单管理模块接口
 * @author chenke
 * @description 酒庄后台订单板块接口
 * @header name:X-Token type:string require:1 desc:登录令牌
 *  */
class Order extends Base
{
    /**
     * @title 商品订单列表
     * @description 后台商品订单数据
     * @url /admin/order/ordshow
     * @method GET
     * @module 商品订单板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:status type:int require:1 default: desc:订单状态 other:(1待付款2待发货3待收货4已完成5已取消6已退)
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对订单编号)
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单数据@
     * @data id:订单ID ordersn:订单编号 username:接收人姓名 sendno:发货单号 contact:接收电话 address:接收地址 fee:运费 status:订单状态(1待付款2待发货3待收货4已完成5已取消6已退) sendtype:配送方式 remark:订单备注
     */
    public function orderList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:'';
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:'';
        $status = (isset($data['status']) && $data['status'])?$data['status']:0;
        $where = [ ['ordtype','=',1] ];
        if($searchCont){
            $where[] = ['contact','like','%'.$searchCont.'%'];
        }
        if($status){
            $where[] = ['status','=',$status];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $return = (new ProdOrder())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates,1);
        return response()->json($return);
    }

    /**
     * @title 订单确认付款和取消订单
     * @description 后台商品订单确认付款和取消订单
     * @url /admin/order/dopay
     * @method POST
     * @module 商品订单板块
     * @author chenke
     * @param name:id type:int require:1 default: other: desc:订单ID
     * @param name:dotype type:int require:1 default: other:(1确认付款2取消订单) desc:操作类型
     * @param name:paytype type:int require:0 default: other:(1为钱包支付、2为反馈钱包支付、3为现金支付) desc:支付类型
     * @return code:错误码
     * @return message:错误提示
     * @return data:成功时为空
     */
    public function confirmPay(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'dotype'=>'required|integer',
            'paytype'=>'integer'
        ];
        $messages = [
            'id.required'=> '请传订单ID',
            'id.integer'=> '订单ID错误',
            'paytype.integer'=> '支付类型错误',
            'dotype.required'=> '请传操作类型',
            'dotype.integer'=> '操作类型错误',
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Transition())->orderPay($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'confirmPay');
        }
        return response()->json($result);
    }

    /**
     * @title 商品订单退款
     * @description 商品订单退款
     * @url /admin/order/reford
     * @method POST
     * @module 商品订单板块
     * @author chenke
     * @param name:id type:int require:1 default: other: desc:订单ID
     * @param name:number type:int require:1 default: other: desc:退款金额
     * @return code:错误码
     * @return message:错误提示
     * @return data:操作成功时为空
     */
    public function refundOrder(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'number'=>'required|integer'
        ];
        $messages = [
            'id.required'=> '请传订单ID',
            'id.integer'=> '订单ID错误',
            'number.required'=> '请填写退款金额',
            'number.integer'=> '退款金额错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new Transition())->orderRefund($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'refundDinOrd');
        }
        return response()->json($result);
    }

    /**
     * @title 订单发货
     * @description
     * @url /admin/order/ordsed
     * @method POST
     * @module 商品订单板块
     * @author chenke
     * @param name:id type:int require:1 default:0 desc:订单ID other:
     * @param name:sendno type:string require:1 default: desc:发货单号 other:
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单ID
     */
    public function orderSend(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'sendno'=>'required|string'
        ];
        $messages = [
            'id.required'=> '请传订单ID',
            'id.integer'=> '订单ID错误',
            'sendno.required'=> '请传发货单号',
            'sendno.string'=> '发货单号错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new ProdOrder())->changeStatus($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'dinOrderChangeStatus');
        }
        return response()->json($result);
    }

    /**
     * @title 积分商品订单列表
     * @description 后台积分商品订单数据
     * @url /admin/order/jordshow
     * @method GET
     * @module 积分商品订单板块
     * @author chenke
     * @param name:limit type:int require:1 default:1 other: desc:每页数据条数
     * @param name:page type:int require:1 default:1 other: desc:当前页数
     * @param name:status type:int require:1 default: desc:订单状态 other:(1待付款2待发货3待收货4已完成5已取消6已退)
     * @param name:searchcontent type:string require:0 default: other: desc:输入框搜索内容(对订单编号)
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单数据@
     * @data id:订单ID ordersn:订单编号 username:接收人姓名 sendno:发货单号 contact:接收电话 address:接收地址 total:订单总金额 fee:运费 status:订单状态(1待付款2待发货3待收货4已完成5已取消6已退) sendtype:配送方式 remark:订单备注
     */
    public function orderJifenList(Request $request){
        $data = $request->all();
        $limit = $data['limit'];
        $page = $data['page'];
        $searchCont = (isset($data['searchcontent']) && $data['searchcontent'])?$data['searchcontent']:'';
        $startDate = (isset($data['startDate']) && $data['startDate'])?$data['startDate']:date('Y-m-d');
        $endDate = (isset($data['endDate']) && $data['endDate'])?$data['endDate']:date('Y-m-d');
        $status = (isset($data['status']) && $data['status'])?$data['status']:0;
        $where = [ ['ordtype','=',2] ];
        if($searchCont){
            $where[] = ['contact','like','%'.$searchCont.'%'];
        }
        if($status){
            $where[] = ['status','=',$status];
        }
        $whereDates = [];
        if($endDate){
            $entime = strtotime($endDate) + 24*3600;
            $endDate = date('Y-m-d',$entime);
        }
        if($startDate && $endDate){
            $whereDates = [$startDate,$endDate];
        }
        $return = (new ProdOrder())->getPageList(['limit'=>$limit,'page'=>$page],$where,$whereDates,2);
        return response()->json($return);
    }

    /**
     * @title 积分订单发货
     * @description
     * @url /admin/order/jordsed
     * @method POST
     * @module 积分商品订单板块
     * @author chenke
     * @param name:id type:int require:1 default:0 desc:订单ID other:
     * @param name:sendno type:string require:1 default: desc:发货单号 other:
     * @return code:错误码
     * @return message:错误提示
     * @return data:订单ID
     */
    public function jifenOrderSend(Request $request){
        $data = $request->post();
        $rules = [
            'id'=>'required|integer',
            'sendno'=>'required|string'
        ];
        $messages = [
            'id.required'=> '请传订单ID',
            'id.integer'=> '订单ID错误',
            'sendno.required'=> '请传发货单号',
            'sendno.string'=> '发货单号错误'
        ];
        $check = $this->checkParams($data,$rules,$messages);
        if($check['code']){
            return response()->json($check);
        }
        $result =  (new ProdOrder())->changeStatus($data);
        if($result['code']){
            $this->logErr($result['code'],$result['message'],'dinOrderChangeStatus');
        }
        return response()->json($result);
    }
}
